
package com.example.maybeaccount;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

import com.example.maybeaccount.Entry.Entrance;
import com.example.maybeaccount.Entry.User;
import com.example.maybeaccount.Mode.AsteriskPasswordTrans;
import com.example.maybeaccount.Mode.ActivityController;
import com.example.maybeaccount.Mode.Check_ok;
import com.example.maybeaccount.Mode.EncrypDES;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;


/**
 * 用户名、密码重置界面
 */
public class ResetActivity extends AppCompatActivity {
    private Check_ok checkOk = new Check_ok();
    TextView textTip;
    private int firstFlag = 0;
    private boolean isTop;
    private int mode;
    public final static int MODE_NAME = 0;
    public final static int MODE_PSWD = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mode = getIntent().getIntExtra("mode",3);
        initBroadcast();
        setContentView(R.layout.activity_reset);
        final EditText name_for_reset = (EditText) findViewById(R.id.countforreset);
        final EditText pwd_for_reset = (EditText)findViewById(R.id.pwdforreset);
        final EditText name_confirm = (EditText) findViewById(R.id.countconfirm);
        final EditText pwd_confirm = (EditText)findViewById(R.id.pwdconfrim);
        final EditText pwdReConfirm = findViewById(R.id.pwd_reconfrim);

        pwd_confirm.setTransformationMethod(new AsteriskPasswordTrans());
        pwd_for_reset.setTransformationMethod(new AsteriskPasswordTrans());
        pwdReConfirm.setTransformationMethod(new AsteriskPasswordTrans());
        textTip = findViewById(R.id.textTip);
        List<User> userList;
        List<User> dataList = new ArrayList<>();
        userList = SqliteDB.getInstance(getApplicationContext()).loadUser();
        Button confirm = (Button)findViewById(R.id.confirm_reset);
        if(mode == MODE_NAME){
            pwd_for_reset.setVisibility(View.GONE);
            pwd_confirm.setVisibility(View.GONE);
            pwdReConfirm.setVisibility(View.GONE);
            textTip.setText("重置用户名");
        }else if(mode == MODE_PSWD){
            name_for_reset.setVisibility(View.GONE);
            name_confirm.setVisibility(View.GONE);
            textTip.setText("重置密码");
        }


        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name="" ;
                String newname="" ;
                String pass="" ;
                String newpwd="" ;
                String newpwdAgain = "";

                String oldname = "";
                String oldpwd = "";

                List<User> userList = userList = SqliteDB.getInstance(getApplicationContext()).loadUser();
                User user = userList.get(0);
                try {
                    EncrypDES encrypDES = new EncrypDES();
                    oldname = encrypDES.decrypt(user.getUsername());
                    oldpwd = encrypDES.double_decrypt(user.getUserpwd());
                }catch (Exception e){
                    Log.d("错误",e.getMessage());
                }

                if(mode == MODE_NAME){
                    name = name_for_reset.getText().toString().trim();
                    newname = name_confirm.getText().toString().trim();
                    pass = oldpwd;
                    newpwd = oldpwd;

                }else if(mode == MODE_PSWD){
                    name = oldname;
                    newname = oldname;
                    pass = pwd_for_reset.getText().toString().trim();
                    newpwd = pwd_confirm.getText().toString().trim();
                    newpwdAgain = pwdReConfirm.getText().toString().trim();
                    if(pass == null ||  newpwd == null || newname == null || newpwdAgain == null ){
                        Toast.makeText(getApplicationContext(),"输入不能为空！",Toast.LENGTH_LONG).show();
                        return;
                    }else if(!newpwdAgain.equals(newpwd)){
                        Toast.makeText(getApplicationContext(),"输入密码不一致！",Toast.LENGTH_LONG).show();
                        pwdReConfirm.setText("");
                        return;
                    }
                }else{
                    name = name_for_reset.getText().toString().trim();
                    newname = name_confirm.getText().toString().trim();
                    pass = pwd_for_reset.getText().toString().trim();
                    newpwd = pwd_confirm.getText().toString().trim();
                }

                try{
                    EncrypDES encrypDES = new EncrypDES();
                    int result=SqliteDB.getInstance(getApplicationContext()).Quer(encrypDES.double_encrypt(pass),encrypDES.encrypt(name));
                    if(result == 1){
                        if(checkOk.password_limit(newpwd)){
                            int mresult = SqliteDB.getInstance(getApplicationContext()).updateUser(encrypDES.encrypt(newname),encrypDES.double_encrypt(newpwd),encrypDES.encrypt(name));
                            if(mresult ==1){
                                if(mode == MODE_NAME){
                                    Toast.makeText(getApplicationContext(),"用户名已重置",Toast.LENGTH_LONG).show();
                                }else if(mode == MODE_PSWD){
                                    Toast.makeText(getApplicationContext(),"密码已重置",Toast.LENGTH_LONG).show();
                                }

                                Intent intent = new Intent(getApplicationContext(), Entrance.class);
                                startActivity(intent);
                            }
                            else {
                                Toast.makeText(getApplicationContext(),"遇到预期外的错误",Toast.LENGTH_LONG).show();
                            }
                        }
                        else{
                            Toast.makeText(getApplicationContext(),"密码设置不合法",Toast.LENGTH_LONG).show();
                            pwd_confirm.setText("");
                        }
                    }
                    else {
                        Toast.makeText(getApplicationContext(),"用户名或者密码错误",Toast.LENGTH_LONG).show();
                    }
                }catch (Exception e){
                    Log.d("错误",e.getMessage());
                }
            }
        });
    }

    /** 后台唤醒处理 */
    @Override
    protected void onResume(){
        super.onResume();
        isTop = true;
        //int memberFlag = getIntent().getIntExtra("member",0);
        if(firstFlag == 1){
            firstFlag = 0;
            Intent intent = new Intent(ResetActivity.this, Entrance.class);
            intent.putExtra("resume","Chart");
            startActivity(intent);
        }else{
            firstFlag = 0;
        }
    }

    /** 广播监听home和menu */
    class InnerRecevier extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {

                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

                if (reason != null) {

                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY) && isTop) {
                        // "Home键被监听"
                        firstFlag = 1;

                    } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS) && isTop) {
                        //"多任务键被监听"
                        firstFlag = 1;

                    }
                }
            }
        }
    }

    private void initBroadcast(){
        /** 广播 */
        this.getWindow().setFlags(0x80000000,0x80000000);
        //创建广播
        InnerRecevier innerReceiver = new InnerRecevier();
        //动态注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //启动广播
        registerReceiver(innerReceiver, intentFilter);
    }
}